package com.xue.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xue.bean.Product;
import com.xue.mapper.ProductMapper;
import com.xue.service.IProductService;
import com.xue.util.StringUtil;

@Service
public class ProductServiceImpl implements IProductService{

	@Resource
	ProductMapper productMapper;
	
	@Override
	public List<Product> selectProduct(Product pro) {
		List<Product> list = productMapper.selectProduct(pro);
		return list;
	}

	@Override
	public Product selectProById(Product pro) {
		Product p = productMapper.selectProById(pro);
		return p;
	}

	@Transactional(rollbackFor = {RuntimeException.class, Error.class})
	@Override
	public Map<String,String> updateProById(Product pro) {
		int rnum = productMapper.updateProById(pro);
		
		Map<String,String> map = new HashMap<String,String>();
		if(rnum>0) {
			map.put("rtnCd", "00");
			map.put("rntMsg", "更新成功");
			
		}else {
			map.put("rtnCd", "9999");
			map.put("rntMsg", "更新");			
		}
		return map;
	}
	@Transactional(rollbackFor = {RuntimeException.class, Error.class})
	@Override
	public Map<String,String> savePro(Product pro) {
		String productId = StringUtil.getId();
		pro.setProductId(productId);
		pro.setValidStsCd("1");		
		pro.setProductSale(0);
		Map<String,String> map = new HashMap<String,String>();
		int rnum = productMapper.savePro(pro);
		if(rnum>0) {
			map.put("rtnCd", "00");
			map.put("rntMsg", "保存成功");
			map.put("productId", productId);			
		}else {
			map.put("rtnCd", "9999");
			map.put("rntMsg", "保存失敗");			
		}
		return map;
	}

	@Override	
	public Map<String,String> delPro(Product pro) {
		int rnum = productMapper.delPro(pro);	
		Map<String,String> map = new HashMap<String,String>();
		if(rnum>0) {
			map.put("rtnCd", "00");
			map.put("rntMsg", "刪除成功");				
		}else {
			map.put("rtnCd", "9999");
			map.put("rntMsg", "刪除失敗");			
		}
		return map;			
	}
}
